package com.app.mymusic.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.app.mymusic.R;
import com.app.mymusic.utils.LogUtil;

/**
 * Created by Administrator on 2016-12-19.
 */

public class CircleBar extends View {
    private Paint circlePaint;
    private Paint textPaint;
    private Paint linePaint;
    private int lineColor;
    private int circleColor;
    private float lineSize;
    private float radius;
    private float currentAngle=36;
    private int visible;

    private boolean isVisible;
    private Paint nPaint;
    private int currentProgress=0;
    private float cx;
    private float cy;
    private float textWith;
    private float textStart;
    private float textHeight;
    private int textEnd;
    private Rect mBound;
    private String progressText;

    public CircleBar(Context context) {
        this(context,null);
    }

    public CircleBar(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }


    public CircleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a=context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleBar,defStyleAttr,0);
        circleColor=a.getColor(R.styleable.CircleBar_bar_color, Color.BLACK);
        lineColor=a.getColor(R.styleable.CircleBar_bar_line_color, Color.WHITE);
        lineSize=a.getDimension(R.styleable.CircleBar_bar_line_size,10);
        radius=a.getDimension(R.styleable.CircleBar_bar_radius,50);
        visible=a.getInt(R.styleable.CircleBar_bar_visibility,0);
        if(visible==0){
            isVisible=false;
        }
        a.recycle();
        initPaint();

    }

    private void initPaint() {
//描边
        linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(5);
        //        背景圆
        circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.WHITE);
        linePaint.setStrokeWidth(5);

        nPaint=new Paint();
        nPaint.setStyle(Paint.Style.STROKE);
        nPaint.setColor(Color.YELLOW);
//        文本
        textPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(30);
        textPaint.setStyle(Paint.Style.FILL);
        mBound=new Rect();
        progressText="100%";
        textPaint.getTextBounds(progressText,0,progressText.length(),mBound);

    }

    @Override
    protected void onDraw(Canvas canvas) {
//        描边

        RectF rect=new RectF(5,5,getWidth()-5,getHeight()-5);
        cx= rect.centerX();
        cy=rect.centerY();
        canvas.drawArc(rect,0,currentAngle,false,linePaint);

        textWith=textPaint.measureText(currentProgress+"%");
        textHeight=textPaint.measureText(currentProgress+"%");

        LogUtil.showLog("textwidth:"+textWith);
        textStart=getWidth()/2-mBound.width()/2+5;
        textEnd=getHeight()/2+mBound.height()/2;

        canvas.drawText(currentProgress+"%",textStart,textEnd,textPaint);
//背景圆
     /*   canvas.drawCircle(75,75,65,circlePaint);
        RectF rect1=new RectF(0,0,150,150);
        canvas.drawRect(rect1,nPaint);*/
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w=measure(widthMeasureSpec,true);
        int h=measure(heightMeasureSpec,false);
        int r=Math.max(w,h);
        setMeasuredDimension(r,r);
    }

    public int measure(int measureSpec,boolean isWith){
        int result;
        int mode=MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode==MeasureSpec.EXACTLY){
            result =size;
        }else{
            int padding=isWith?getPaddingLeft()+getPaddingRight():getPaddingTop()+getPaddingBottom();
            result=isWith?padding+mBound.width():padding+mBound.height();

        }
        return  result;
    }
    public void start(int  i)
    {
        currentProgress=i;
        currentAngle= (float) (i*3.6);
        invalidate();
    }
    public void setProgress(int i){
        currentProgress=i;
        currentAngle= (float) (i*3.6);
        invalidate();
    }
}
